package com.ruoyi.course.service.impl;

import com.ruoyi.common.constant.SysConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.course.domain.SysStudentMistake;
import com.ruoyi.course.domain.SysStudentNote;
import com.ruoyi.course.mapper.SysStudentNoteMapper;
import com.ruoyi.course.service.ISysStudentNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 学员内容Service业务层处理
 * 
 * @author YJF
 */
@Service
public class SysStudentNoteServiceImpl implements ISysStudentNoteService
{
    @Autowired
    private SysStudentNoteMapper sysStudentNoteMapper;

    /**
     * 查询成绩列表
     * 
     * @param sysStudentNote 学员参数
     * @return 成绩列表
     */
    @Override
    public List<SysStudentNote> selectSysStudentNoteList(SysStudentNote sysStudentNote)
    {
        if(StringUtils.isNull( sysStudentNote.getDelFlag())){
            sysStudentNote.setDelFlag(SysConstants.CONSTANT_STR0);
        }
        List<SysStudentNote> list = sysStudentNoteMapper.selectSysStudentNoteList(sysStudentNote);
        for(SysStudentNote l :list){
            //时长
            if (l.getBeginTime() != null && l.getFinishTime() != null) {
                String diffTime = DateUtils.getTaskDatePoor(l.getFinishTime(),l.getBeginTime());
                l.setElapsedTime(diffTime);
            }
            //成绩等级
            l.setGrade(getGrade(l.getScore(),l.getPaperScore()));
        }
        return list;
    }

    /**
     * 成绩等级。
     * @param  grade 总成绩
     * @param  score 得分成绩
     * @return 等级结果
     */
    public String getGrade(String score,String grade)
    {
        //得分成绩
        BigDecimal bScore = new BigDecimal(score);
        BigDecimal bdScore = bScore.setScale(2, BigDecimal.ROUND_HALF_UP);
        //总成绩
        BigDecimal bGrade = new BigDecimal(grade);
        BigDecimal bdGrade = bGrade.setScale(2, BigDecimal.ROUND_HALF_UP);
        //优
        BigDecimal excellent = bdGrade.multiply(new BigDecimal("0.9"));
        //良
        BigDecimal good = bdGrade.multiply(new BigDecimal("0.8"));
        //中
        BigDecimal medium = bdGrade.multiply(new BigDecimal("0.7"));
        //及格
        BigDecimal pass = bdGrade.multiply(new BigDecimal("0.6"));

        String gradeLevel = "";
        if(bdScore.compareTo(excellent) >= 0){
            gradeLevel ="优";
        }else if(bdScore.compareTo(good) >= 0){
            gradeLevel ="良";
        }else if(bdScore.compareTo(medium) >= 0){
            gradeLevel ="中";
        }else if(bdScore.compareTo(pass) >= 0){
            gradeLevel ="及格";
        }else {
            gradeLevel ="不及格";
        }

        return gradeLevel;
    }

    /**
     * 查询错题列表
     * @param sysStudentMistake 错题参数
     * @return 错题列表
     */
    @Override
    public List<SysStudentMistake> selectMistakeList(SysStudentMistake sysStudentMistake)
    {
        //当前登录用户ID
        Long userId = SecurityUtils.getUserId();
        //非管理员只能看到自己的错题
        if (!SysUser.isAdmin(userId)) {
            sysStudentMistake.setUserId(userId);
        }
        sysStudentMistake.setDelFlag(SysConstants.CONSTANT_STR0);
        sysStudentMistake.setCorrectStatus(SysConstants.CONSTANT_STR2);
        sysStudentMistake.setTopicStatus(SysConstants.CONSTANT_INT0);
        List<SysStudentMistake> list = sysStudentNoteMapper.selectMistakeList(sysStudentMistake);
        return list;
    }
}
